const mongoose = require('mongoose');
const request = require('request');

var User = mongoose.model('client');
var Order=mongoose.model("order");

var config = require('../../../core/server/config/config.js');

exports.login = function (req,res) {
    var cname=req.body.cname;
    var cpassword=req.body.cpassword;
    User.findOne({cname:cname,password:cpassword},function(err,user){
       if(!err&&user){
           req.session.client=user;
           res.json({client:user,msg:"SUCCESS"});
       }
        else{
           res.json({msg:"FAILURE"});
       }
    });
};

exports.findByName = function(req, res){
    var cname=req.params.cname;
    User.findOne({cname:cname},function(err,user){
       if(!err&&user){
           res.json({client:user,msg:"SUCCESS"})
       }
        else{
           res.json({msg:"FAILURE"});
       }
    });
};

exports.regist = function (req, res) {
    var client=req.body;
    var user=new User(client);
    user.createDate=new Date();
    user.save(function(err){
        if(!err){
            res.json({msg:"SUCCESS"});
        }
        else{
            res.json({msg:"FAILURE"});
        }
    });
};

exports.update = function (req, res) {
    var client=req.body;
    var user0=req.session.client;
    User.findOne({_id:user0._id},function(err,user1){
        user1.address=client.address;
        user1.ctel=client.ctel;
        user1.save(function(err){
             if(!err){
                 req.session.client.ctel=user1.ctel;
                 req.session.client.address=user1.address;
                 res.json({msg:"SUCCESS"});
             }
            else{
                 res.json({msg:"FAILURE"});
             }
        });
    })
};

exports.getComplain = function (req, res) {
    var user0=req.session.client;
    request.get({url:config.adminWS.baseUrl + config.adminWS.complaintList+"/"+user0._id},function(err,response,body){
        if(!err&&response.statusCode==200){
            var bodies=JSON.parse(body);
            var map={};
            var is=[];
            for(var i in bodies){
                is.push(i);
            }
            Order.find({_id:{$in:is}}).populate("merchant").populate("client")
                .exec(function(err,orders){
                    var resultOrders=orders;
                   for(var j in bodies){
                       for(var k in resultOrders){
                           if(resultOrders[k]._id==j){
                               switch (bodies[j]){
                                   case 0:
                                       resultOrders[k].compainResult='Unhandled';
                                       break;
                                   case 1:
                                       resultOrders[k].compainResult='Dismiss Complaint';
                                       break;
                                   case 2:
                                       resultOrders[k].compainResult='Deduct Deposit';
                                       break;
                                   case 3:
                                       resultOrders[k].compainResult='Blacklisted';
                                       break;
                               }

                           }
                       }
                   }
                    res.json({msg:"SUCCESS",orders:resultOrders});
                });
        }
        else {
            res.json({msg:"FAILURE"});
        }
    });
};

exports.logout = function (req, res) {
    req.session.client=null;
    res.json({msg:"SUCCESS"});
};

exports.getSession = function(req,res){
    res.json({msg:"SUCCESS",client:req.session.client});
}

exports.logout = function(req,res){
    req.session.client=null;
    res.json({msg:"SUCCESS"});
}